//导入express模块
const express = require("express")
//创建主应用
const app = express()
//引入path模块
const path = require("path")
//导入http模块,并创建server
const server = require("http").Server(app)
// 导入chat模块
const chat = require('./modules/chat')
// chat聊天
chat(server);

// https
var fs = require("fs")
var https = require("https")

//根据项目的路径导入生成的证书文件
var privateKey = fs.readFileSync(
  path.join(__dirname, "./config/ssl/private.pem"),
  "utf8"
)
var certificate = fs.readFileSync(
  path.join(__dirname, "./config/ssl/file.crt"),
  "utf8"
)
var credentials = { key: privateKey, cert: certificate }

var httpsServer = https.createServer(credentials, app)
httpsServer.listen(8001, () => {
//console.log("https服务已在8001启动")
})

//加载静态资源
app.use('/cake-pro', express.static(path.join(__dirname, '/cake-pro')))
app.use(express.static(__dirname + "/public"))
app.use(express.static(__dirname + "/views"))
//设置访问主应用首页
app.get("/", (req, res) => {
  res.send("this is the first page!")
})

app.use(express.urlencoded({ extended: true }))
// 错误中间件
app.use((err, req, res, next) => {
  //err 得到的中间件传递过来的错误
//console.log(err)
  //设置http协议响应的状态码
  res.status(500).send({ code: 500, msg: "服务器端错误" })
})
//引入用户登录路由
const login = require("./route/login.js")
app.use("/v1/login", login)
// 用户注册路由
const register = require("./route/register.js")
app.use("/v1/register", register)
// 用户修改密码
const changePwd = require("./route/changePwd.js")
app.use("/v1/changepwd", changePwd)
// 商品列表
const product = require("./route/product.js")
app.use("/v1/product", product)
//商品的详情页
const detail = require("./route/detail.js")
app.use("/v1/detail", detail)
//搜索页面
const search = require("./route/search.js")
app.use("/v1/search", search)
// 优惠券接口
const coupon = require("./route/coupon")
app.use("/v1/coupon", coupon)
//积分接口
const integral = require("./route/integral")
app.use("/v1/integral", integral)
// 购物车接口
const card = require("./route/card.js")
app.use("/v1/card", card)
// 订单接口
const order=require("./route/order")
app.use("/v1/order",order)
// 用户地址
const address = require("./route/address.js")
app.use("/v1/userAddress", address)
// 修改用户数据
const material = require("./route/material.js")
app.use("/v1/material", material)
// 主页
const home = require("./route/home.js")
app.use("/v1/home", home)
//我的评论接口
const my_comments = require("./route/my_comments")
app.use("/v1/my_comments",my_comments)
//商品的评论的接口
const comments = require("./route/comment")
app.use("/v1/comments",comments)
// 支付接口
const alipay = require("./route/alipay/alipay.js")
app.use("/v1/alipay", alipay)

// admin接口
const admin = require("./route/admin/manager")
app.use("/v1/admin", admin)

server.listen(8000, () => {
console.log("8000服务器启动成功...")
})

